//
//  MaineJournalAppDelegate.h
//  MaineJournal
//
//  Created by Administrator on 8/15/11.
//  Copyright 2011 University of Maine. All rights reserved.
//

#import "Database.h"
#import "Task.h"

#define kLibraryExtendedAttribute @"comicflow.identifier"
#define kLibraryThumbnailWidth 135
#define kLibraryThumbnailHeight 180

@class LibraryUpdater;

@protocol LibraryUpdaterDelegate <NSObject>
- (void) libraryUpdaterWillStart:(LibraryUpdater*)library;
- (void) libraryUpdaterDidContinue:(LibraryUpdater*)library progress:(float)progress;
- (void) libraryUpdaterDidFinish:(LibraryUpdater*)library;
- (void) libraryUpdaterDidCancel:(LibraryUpdater*)library;
@end

#if __STORE_THUMBNAILS_IN_DATABASE__

@interface Thumbnail : DatabaseObject
@property(nonatomic, copy) NSData* data;
@end

#endif

@interface Comic : DatabaseObject
@property(nonatomic) DatabaseSQLRowID collection;  // May be 0
@property(nonatomic, copy) NSString* name;
#if __STORE_THUMBNAILS_IN_DATABASE__
@property(nonatomic) DatabaseSQLRowID thumbnail;
#else
@property(nonatomic, copy) NSString* thumbnail;
#endif
@property(nonatomic) NSTimeInterval time;
@property(nonatomic) int status;  // -1: new, 0: normal, 1+: reading
@end

@interface Collection : DatabaseObject
@property(nonatomic, copy) NSString* name;
#if __STORE_THUMBNAILS_IN_DATABASE__
@property(nonatomic) DatabaseSQLRowID thumbnail;
#else
@property(nonatomic, copy) NSString* thumbnail;
#endif
@property(nonatomic) NSTimeInterval time;
@property(nonatomic, readonly) int status;  // -1: new, 0: normal, 1+: reading
@property(nonatomic) int scrolling;
@end

@interface Collection ()
@property(nonatomic, retain) NSArray* comics;
@end

@interface LibraryConnection : DatabaseConnection
+ (NSString*) libraryRootPath;
+ (NSString*) libraryApplicationDataPath;
+ (NSString*) libraryDatabasePath;
+ (LibraryConnection*) mainConnection;  // For main thread only
- (NSArray*) fetchAllComicsByName;
- (NSArray*) fetchAllComicsByDate;
- (NSArray*) fetchAllComicsByStatus;
- (NSArray*) fetchComicsInCollection:(Collection*)collection;
- (NSArray*) fetchAllCollectionsByName;
- (BOOL) updateStatus:(int)status forComicsInCollection:(Collection*)collection;
- (BOOL) updateStatusForAllComics:(int)status;
@end

@interface LibraryUpdater : NSObject {
@private
  id<LibraryUpdaterDelegate> _delegate;
  CGImageRef _comicBackgroundImageRef;
  CGImageRef _comicScreenImageRef;
  CGImageRef _collectionBackgroundImageRef;
  CGImageRef _collectionScreenImageRef;
  NSData* _fakeData;
  Task* _updateTask;
}
@property(nonatomic, readonly, getter=isUpdating) BOOL updating;
@property(nonatomic, assign) id<LibraryUpdaterDelegate> delegate;
+ (LibraryUpdater*) sharedUpdater;
- (void) startUpdating:(BOOL)force;
- (void) cancelUpdating;
@end
